<template>
	<el-container>
		<el-container>
			<el-header>代码生成</el-header>
			<el-main>
				<el-row :gutter="24">
					<el-col :lg="6">
						<el-form-item label="选择数据表" prop="tableName">
							<el-select
								v-model="form.tableName"
								filterable
								remote
								reserve-keyword
								placeholder="数据库表名"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width:100%"
							>
								<el-option v-for="item in options" :key="item.tableName"
										   :label="item.tableName+item.tableComment"
										   :value="item.tableName"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :lg="14">
						<el-checkbox-group v-model="form.files">
							<el-checkbox-button label="controller">Controller类</el-checkbox-button>
							<el-checkbox-button label="bean">实体类</el-checkbox-button>
							<el-checkbox-button label="service">Service类</el-checkbox-button>
							<el-checkbox-button label="mapper">Mapper类</el-checkbox-button>
							<el-checkbox-button label="xml">mybatis Xml</el-checkbox-button>
							<el-checkbox-button label="apiJs">VUE前台接口</el-checkbox-button>
							<el-checkbox-button label="vueManage">VUE记录管理页</el-checkbox-button>
							<el-checkbox-button label="vueSave">VUE编辑页</el-checkbox-button>
							<el-checkbox-button label="vueDetails">VUE详情页</el-checkbox-button>
						</el-checkbox-group>
					</el-col>
					<el-col :lg="4">
						<el-button type="primary" @click="doGenCode()">生成</el-button>
					</el-col>
				</el-row>
				<el-divider content-position="center">生成进度</el-divider>
				<div v-if="statusList.cGet.flag=='1'">
					<span>{{ statusList.cGet.title }}</span>
					<el-tag type="success" v-if="statusList.cGet.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.cSet.flag=='1'">
					<span>{{ statusList.cSet.title }}</span>
					<el-tag type="success" v-if="statusList.cSet.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.bean.flag=='1'">
					<span>{{ statusList.bean.title }}</span>
					<el-tag type="success" v-if="statusList.bean.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.mapper.flag=='1'">
					<span>{{ statusList.mapper.title }}</span>
					<el-tag type="success" v-if="statusList.mapper.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.xml.flag=='1'">
					<span>{{ statusList.xml.title }}</span>
					<el-tag type="success" v-if="statusList.xml.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.service.flag=='1'">
					<span>{{ statusList.service.title }}</span>
					<el-tag type="success" v-if="statusList.service.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.apiJs.flag=='1'">
					<span>{{ statusList.apiJs.title }}</span>
					<el-tag type="success" v-if="statusList.apiJs.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.manage.flag=='1'">
					<span>{{ statusList.manage.title }}</span>
					<el-tag type="success" v-if="statusList.manage.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.save.flag=='1'">
					<span>{{ statusList.save.title }}</span>
					<el-tag type="success" v-if="statusList.save.status=='1'">已完成</el-tag>
				</div>
				<div v-if="statusList.details.flag=='1'">
					<span>{{ statusList.details.title }}</span>
					<el-tag type="success" v-if="statusList.details.status=='1'">已完成</el-tag>
				</div>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
export default {
	name: 'Auto Gen Code',
	data() {
		return {
			loading: false,
			form: {
				files: ['controller', 'bean', 'service', 'mapper', 'xml', 'apiJs','vueManage', 'vueSave', 'vueDetails'],
				tableName: '',
			},
			options: [],
			statusList: {
				cGet: {
					flag: '0',
					title: '正在生成Controller Get控制器代码....',
					status: '0'
				},
				cSet: {
					flag: '0',
					title: '正在生成Controller Set控制器代码....',
					status: '0'
				},
				bean: {
					flag: '0',
					title: '正在生成Bean实体类代码....',
					status: '0'
				},
				mapper: {
					flag: '0',
					title: '正在生成Mapper服务类代码....',
					status: '0'
				},
				xml: {
					flag: '0',
					title: '正在生成MyBatis Xml Sql....',
					status: '0'
				},
				service: {
					flag: '0',
					title: '正在生成Service服务类代码....',
					status: '0'
				},
				apiJs:{
					flag: '0',
					title: '正在生成前台Vue Api 接口文件....',
					status: '0'
				},
				manage:{
					flag: '0',
					title: '正在生成前台记录管理页面文件....',
					status: '0'
				},
				save:{
					flag: '0',
					title: '正在生成前台记录编辑页面文件....',
					status: '0'
				},
				details:{
					flag: '0',
					title: '正在生成前台记录详情页面文件....',
					status: '0'
				}

			}

		}
	},
	mounted() {
		this.getGenTableNameList();
	},
	methods: {
		async getGenTableNameList() {
			let res = await this.$API.gen.sysGenDb.getGenTableNameList.get();
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteMethod(keyword) {
			let res = await this.$API.gen.sysGenDb.getGenTableNameList.get({keyword: keyword});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async doGenCode() {
			if (this.form.tableName == undefined || this.form.tableName == null || this.form.tableName == '') {
				this.$alert("请选择数据库表", "提示", {type: 'error'});
				return false;
			} else {
				if (this.form.files.includes("controller")) {
					this.statusList.cGet.flag = "1";
					let res0 = await this.$API.gen.sysGen.createControllerGetFile.get({tableName: this.form.tableName});
					if (res0.code == 200) {
						this.statusList.cGet.status = "1";
					} else {
						this.$alert(res0.message, "提示", {type: 'error'})
					}
					this.statusList.cSet.flag = "1";
					let res1 = await this.$API.gen.sysGen.createControllerSetFile.get({tableName: this.form.tableName});
					if (res1.code == 200) {
						this.statusList.cSet.status = "1";
					} else {
						this.$alert(res1.message, "提示", {type: 'error'})
					}
				}
				if (this.form.files.includes("bean")) {
					this.statusList.bean.flag = "1";
					let res = await this.$API.gen.sysGen.createBeanFile.get({tableName: this.form.tableName});
					if (res.code == 200) {
						this.statusList.bean.status = "1";
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
				if (this.form.files.includes("service")) {
					this.statusList.service.flag = "1";
					let res = await this.$API.gen.sysGen.createServiceFile.get({tableName: this.form.tableName});
					if (res.code == 200) {
						this.statusList.service.status = "1";
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
				if (this.form.files.includes("mapper")) {
					this.statusList.mapper.flag = "1";
					let res = await this.$API.gen.sysGen.createMapperFile.get({tableName: this.form.tableName});
					if (res.code == 200) {
						this.statusList.mapper.status = "1";
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
				if (this.form.files.includes("xml")) {
					this.statusList.xml.flag = "1";
					let res = await this.$API.gen.sysGen.createMapperXmlFile.get({tableName: this.form.tableName});
					if (res.code == 200) {
						this.statusList.xml.status = "1";
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
				if (this.form.files.includes("apiJs")) {
					this.statusList.apiJs.flag = "1";
					let res = await this.$API.gen.sysGen.createVueApiJsFile.get({tableName: this.form.tableName});
					if (res.code == 200) {
						this.statusList.apiJs.status = "1";
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
				if (this.form.files.includes("vueManage")) {
					this.statusList.manage.flag = "1";
					let res = await this.$API.gen.sysGen.createVueManageFile.get({tableName: this.form.tableName});
					if (res.code == 200) {
						this.statusList.manage.status = "1";
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
				if (this.form.files.includes("vueSave")) {
					this.statusList.save.flag = "1";
					let res = await this.$API.gen.sysGen.createVueSaveFile.get({tableName: this.form.tableName});
					if (res.code == 200) {
						this.statusList.save.status = "1";
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
				if (this.form.files.includes("vueDetails")) {
					this.statusList.details.flag = "1";
					let res = await this.$API.gen.sysGen.createVueDetailsFile.get({tableName: this.form.tableName});
					if (res.code == 200) {
						this.statusList.details.status = "1";
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			}
		}
	}
}
</script>

<style>
</style>
